import moment from '../../utils/moment';
import request from '../../common/request';
import {xzl} from '../../common/config';

Page({

    data: {
      commentList:[],
      fixedList:[],
      temp_list:[],
      time:moment().format(),
      selectRange:['所有评论','我发出的','评论我的'],
      selectIndex:0
    },

    // 获取评论
    async getCommentList(){
      const token = wx.getStorageSync('cookie');
      const URL = `${xzl}/getCommentList?access_token=${token}`;
      const res = await request(URL);
      if(res.code!==200){
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false,
          confirmText: '知道了',
          confirmColor: '#3CC51F',
        });
        return;
      }
      this.setData({
        commentList:res.data,
        temp_list:res.data,
        fixedList:res.data
      })
    },

    // 处理选择框
    handleSelect(e){
      console.log(e.detail.value);
      const index = Number(e.detail.value);
      const myId = wx.getStorageSync('userId');
      let {temp_list,fixedList} = this.data;
        
      this.setData({
        selectIndex:index
      })

      // 对数据进行分类处理

      switch(index){
        case 1:
          const arr = temp_list.filter(item=>item.userId==myId);
          this.setData({
            commentList:arr
          })
          break;
        case 2:
          const arr1 = temp_list.filter(item=>item.sellerUserId==myId);
          this.setData({
            commentList:arr1
          })
          break;
        default:
          this.setData({
            commentList:fixedList
          })
      }
    },

    //跳转到商品详情页
    navToGoodsDetail(e){
      console.log(e.currentTarget.dataset.id);

      const goodsId = e.currentTarget.dataset.id;

      wx.navigateTo({
        url: `/pages/goods_detail/goods_detail?goodsId=${goodsId}`,
      });
    },

    onLoad(){
      this.getCommentList()
    }
})